import axios from 'axios'
import {ElMessage} from "element-plus"
const baseURL ="http://rap2api.taobao.org/app/mock/308829"

//创建axios实例

const service=axios.create({
    baseURL,
    timeout:1000,
})


// 请求拦截
service.interceptors.request.use(
    config=>{
        const token="token string...."
        if( !config.headers) {
            config.headers = {}
        }
        if(token) {
            config.headers.Authorization = token
        }
        return config
    },
    error=>{
        ElMessage.error(error.message||"请求拦截失败")
        return Promise.reject(error)
    }
)
// 响应应拦截
service.interceptors.response.use(
    response=>{
        //response.status返回http的响应码
        if(response.status>=200 && response.status<300){
            //response.data服务器返回的数据
            // const serverResponseData=response.data
            const {code,data,message}=response.data
            if(code===200){
                return data
            }
            const err=new Error(message||"响应拦截处理异常")
            ElMessage.error(message||"相应拦截处理有异常！")
            return Promise.reject(err)
        }
    },
    error=>{
        ElMessage.error(error.message||"请求拦截失败")
        return Promise.reject(error)
    }
)


export default service